﻿Imports FSUIPC
Module Average_Fuel
    Dim Flow_Total As Double
    Dim Tick_Count As Integer
    Dim Formatted_Eng1 As Double = 0
    Dim Formatted_Eng2 As Double = 0
    Dim Formatted_Eng3 As Double = 0
    Dim Formatted_Eng4 As Double = 0
    Dim Total_fuel As Double = 0

#Region "FSUIPC Addresses Screen 5"
    ' Fuel Flow for each engine.
    Dim Eng1_Fuel_Flow As Offset(Of Double) = New FSUIPC.Offset(Of Double)("Screen5", &H918)
    Dim Eng2_Fuel_Flow As Offset(Of Double) = New FSUIPC.Offset(Of Double)("Screen5", &H9B0)
    Dim Eng3_Fuel_Flow As Offset(Of Double) = New FSUIPC.Offset(Of Double)("Screen5", &HA48)
    Dim Eng4_Fuel_Flow As Offset(Of Double) = New FSUIPC.Offset(Of Double)("Screen5", &HAE0)
    ' Fuel Tank Capacities.
    Dim Fuel_Level_Center_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB78)
    Dim Fuel_Level_Left_Main_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB80)
    Dim Fuel_Level_Left_Aux_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB88)
    Dim Fuel_Level_Left_Tip_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB90)
    Dim Fuel_Level_Right_Main_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB98)
    Dim Fuel_Level_Right_Aux_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HBA0)
    Dim Fuel_Level_Right_Tip_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HBA8)
    Dim Fuel_Level_Center2_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1248)
    Dim Fuel_Level_Center3_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1250)
    Dim Fuel_Level_External1_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1258)
    Dim Fuel_Level_External2_Cap As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1260)
    ' Fuel Level in Percentage
    Dim Fuel_Left_Tip_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB8C)
    Dim Fuel_Left_Aux_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB84)
    Dim Fuel_Left_Main_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB7C)

    Dim Fuel_Right_Tip_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HBA4)
    Dim Fuel_right_Aux_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB9C)
    Dim Fuel_Right_Main_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB94)

    Dim Fuel_Center1_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &HB74)
    Dim Fuel_Center2_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1244)
    Dim Fuel_Center3_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H124C)

    Dim Fuel_Ext1_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H1254)
    Dim Fuel_Ext2_Percent As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen5", &H125C)


#End Region

#Region "Screem 5"
    Public Sub Display_Screen()
        Dim img As New System.Drawing.Bitmap(160, 43)
        Dim screen As Drawing.Graphics = Graphics.FromImage(img)
        Try
            FSUIPC.FSUIPCConnection.Process("Screen5")

            ' Get Values.
            Dim Eng1_Flow As Double = (Eng1_Fuel_Flow.Value / 6.7) ' Convert from PPH to G/PH.
            Dim Eng2_Flow As Double = (Eng2_Fuel_Flow.Value / 6.7) ' Convert from PPH to G/PH.
            Dim Eng3_Flow As Double = (Eng3_Fuel_Flow.Value / 6.7) ' Convert from PPH to G/PH.
            Dim Eng4_Flow As Double = (Eng4_Fuel_Flow.Value / 6.7) ' Convert from PPH to G/PH.

            ' Format Numbers.
            Formatted_Eng1 = Math.Round(Eng1_Flow, 1)
            Formatted_Eng2 = Math.Round(Eng2_Flow, 1)
            Formatted_Eng3 = Math.Round(Eng3_Flow, 1)
            Formatted_Eng4 = Math.Round(Eng4_Flow, 1)

            ' Limit the amount of values = reset every 10 tick counts to supress OverFlow error else carry on.
            If Tick_Count = 20 Then
                Tick_Count = 0
                Flow_Total = 0
            End If

            ' Total Fuel flow for all engines.
            Flow_Total = Flow_Total + Eng1_Flow + Eng2_Flow + Eng3_Flow + Eng4_Flow

            'Total Fuel in Gallons for all tanks.
            Total_fuel = (Fuel_Level_Left_Tip_Cap.Value / 100 * (Fuel_Left_Tip_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_Left_Aux_Cap.Value / 100 * (Fuel_Left_Aux_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_Left_Main_Cap.Value / 100 * (Fuel_Left_Main_Percent.Value * 100 / (128 * 65536))) _
             + (Fuel_Level_Right_Tip_Cap.Value / 100 * (Fuel_Level_Right_Tip_Cap.Value * 100 / (128 * 65536))) + (Fuel_Level_Right_Aux_Cap.Value / 100 * (Fuel_right_Aux_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_Right_Main_Cap.Value / 100 * (Fuel_Right_Main_Percent.Value * 100 / (128 * 65536))) _
             + (Fuel_Level_Center_Cap.Value / 100 * (Fuel_Center1_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_Center2_Cap.Value / 100 * (Fuel_Center2_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_Center3_Cap.Value / 100 * (Fuel_Center3_Percent.Value * 100 / (128 * 65536))) _
             + (Fuel_Level_External1_Cap.Value / 100 * (Fuel_Ext1_Percent.Value * 100 / (128 * 65536))) + (Fuel_Level_External2_Cap.Value / 100 * (Fuel_Ext2_Percent.Value * 100 / (128 * 65536)))

            Tick_Count += 1

            Dim Flow_AVG As Double = Math.Round(Flow_Total / Tick_Count, 1)
            Dim Distance_For_Fuel As Double = Total_fuel / Flow_AVG ' Work out time for current fuel levels using Fuel Flow Average.

            ' Engines 1 & 2.
            screen.DrawString("Eng 1:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(1, 2, 160, 43))
            screen.DrawString("Eng 2:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(1, 12, 160, 43))
            screen.DrawString("Eng 3:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(1, 22, 160, 43))
            screen.DrawString("Eng 4:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(1, 32, 160, 43))

            screen.DrawString("AVG FLOW:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(78, 2, 160, 43))

            ' Display Data.
            screen.DrawString(Formatted_Eng1.ToString, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(30, 2, 160, 43))
            screen.DrawString(Formatted_Eng2.ToString, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(30, 12, 160, 43))
            screen.DrawString(Formatted_Eng3.ToString, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(30, 22, 160, 43))
            screen.DrawString(Formatted_Eng4.ToString, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(30, 32, 160, 43))

            screen.DrawString(Flow_AVG.ToString & " GPH", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(78, 12, 160, 43))
            screen.DrawString(FormatNumber(Distance_For_Fuel, 2).ToString & " H.M", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(80, 22, 160, 43))

        Catch exFSUIPC As FSUIPCException
            MainTimer.Stop()
            CheckTimer.Start()
        Finally
            G15FS9_Main.lcd.DrawLCD(img)
            G15FS9_Main.lcd.UpdateLCD()
        End Try
    End Sub
#End Region

End Module

